cmake_minimum_required(VERSION 2.8)
project(lich4api C)

# relative path

string(LENGTH "${CMAKE_SOURCE_DIR}/" SOURCE_PATH_SIZE)
add_definitions("-DSOURCE_PATH_SIZE=${SOURCE_PATH_SIZE}")

# set lich version
set (LICH_VERSION_MAJOR 1)
set (LICH_VERSION_MINOR 0)

# set(CMAKE_BUILD_TYPE Release)
# set (CMAKE_C_FLAGS_CONFIG "-g -W -Wall -Werror --Werror=return-type -D_GNU_SOURCE -D_REENTRANT")

# TODO for Ubuntu etc
if (EXISTS "${CMAKE_SOURCE_DIR}/__testing__")
    set (CMAKE_C_FLAGS "-fPIC -g -w -Wall -Werror -D_GNU_SOURCE -D_REENTRANT -rdynamic")
else()
    # for centos6,centos7 etc
    set (CMAKE_C_FLAGS "-fPIC -g -W -Wall -Werror -D_GNU_SOURCE -D_REENTRANT -rdynamic")
endif()

set (CMAKE_C_LIBS lich4 pthread crypt crypto uuid aio ssl rt m sqlite3)


# message("CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}")

find_package(BISON)
find_package(FLEX)
find_package(Threads)
find_package(aio)

set(LICHAPI "lich4api")

set(LICH_SRC "${CMAKE_SOURCE_DIR}/../..")
set(LUA_SRC "${CMAKE_SOURCE_DIR}/lua_src")

# ADD_INCLUDE_DIRECTORIES
include_directories(
        ${LUA_SRC}

        ${LICH_SRC}/include
        ${LICH_SRC}/config/include
        ${LICH_SRC}/build
        ${LICH_SRC}/ylib/include
        ${LICH_SRC}/ynet/include
        ${LICH_SRC}/ynet/sock/include
        ${LICH_SRC}/cluster/include
        ${LICH_SRC}/schedule/include
        ${LICH_SRC}/storage/storage
        ${LICH_SRC}/storage/controller
        ${LICH_SRC}/storage/chunk
        ${LICH_SRC}/storage/replica
        ${LICH_SRC}/lichd/include
        ${LICH_SRC}/utils/include
        ${LICH_SRC}/utils/src
        ${LICH_SRC}/usr/include
)

# SET_TARGET_PROPERTIES(lich4 PROPERTIES COMPILE_FLAGS " -laio")

LINK_DIRECTORIES(
        "/usr/local/lib"
        "${LICH_SRC}/cb"
)

file(GLOB LICH4API_SRC_FILES ${LUA_SRC} *.c)

list(APPEND LICH4API_SRC_FILES
        ${CMAKE_SOURCE_DIR}/${LICHAPI}/lich4api.c
        )
add_library(${LICHAPI} SHARED ${LICH4API_SRC_FILES})
target_link_libraries(${LICHAPI} ${CMAKE_C_LIBS})

add_custom_target(rename
        DEPENDS ${LICHAPI}
        COMMAND mv lib${LICHAPI}.so ${LICHAPI}.so
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/cb
        )

add_custom_target(test
        DEPENDS rename
        COMMAND lua ../${LICHAPI}/${LICHAPI}.lua
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/cb
        )
